package com.group9.handinhand.service.so;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.group9.handinhand.dao.model.User;
import com.group9.handinhand.service.so.UserEnums.UserPrivacy;
import com.group9.handinhand.service.so.UserEnums.UserStatus;
/**
 * Class for service of user
 * @author Egeid
 *
 */
public class UserSO extends AbstractBaseSO implements UserDetails {
	
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String surname;
	private String username;
	private String email;
	private String password;
	private UserStatus status;
	private String mobile;
	private String phone;
	private String address;
	private String city;
	private String country;
	private String info;
	private byte [] picture;
	private UserPrivacy userPrivacy;

	private Set<SocialUserSO> socialUsers = new HashSet<SocialUserSO>();
	/**
	 * converts user model object
	 * @param ignoreProperties
	 * @return
	 */
	public User convertToModelObject(String... ignoreProperties) {
		User model = (User) convert(new User(),
			(String[]) ArrayUtils.addAll(ignoreProperties, new String[]{"socialUsers"}));
		if( ! Arrays.asList(ignoreProperties).contains("socialUsers"))
		for (SocialUserSO socialUser : socialUsers) {
			model.getSocialUsers().add(socialUser.convertToModelObject());
		}
		return model;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	/*public String getUsername() {
		return username;
	}*/
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	/*public String getPassword() {
		return password;
	}*/
	public void setPassword(String password) {
		this.password = password;
	}
	
	public UserStatus getStatus() {
		return status;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}	
	public byte[] getPicture() {
		return new org.apache.commons.codec.binary.Base64().decode(picture);
	}

	public void setPicture(byte[] picture) {
		this.picture = new org.apache.commons.codec.binary.Base64().encode(picture);
	}

	public Set<SocialUserSO> getSocialUsers() {
		return socialUsers;
	}

	public void setSocialUsers(Set<SocialUserSO> socialUsers) {
		this.socialUsers = socialUsers;
	}
	
	public UserPrivacy getUserPrivacy() {
		return userPrivacy;
	}

	public void setUserPrivacy(UserPrivacy userPrivacy) {
		this.userPrivacy = userPrivacy;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> listAuthorities = new ArrayList<GrantedAuthority>();
		listAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
 		return listAuthorities;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}
	
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public String toString() {
		return "UserSO [name=" + name + ", surname=" + surname + ", username="
				+ username + ", email=" + email + ", password=" + password
				+ ", status=" + status + ", mobile=" + mobile + ", phone="
				+ phone + ", address=" + address + ", city=" + city
				+ ", country=" + country + ", info=" + info + ", picture="
				+ picture + ", userPrivacy=" + userPrivacy + ", socialUsers="
				+ socialUsers + "]";
	}

}
